// pages/selfAssessment/videoAssessment/highRiskInfo.js
const api = require('../../../config/api')
const request = require('../../../utils/request')
const { parse, stringify, getTodayDate } = require('../../../utils/util')
let app = getApp()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    prenatalFactorsList: [
      {
        checked: false,
        name: '孕妇分娩年龄16岁以下或35岁以上',
      },
      {
        checked: false,
        name: '吸烟习惯',
      },
      {
        checked: false,
        name: '肥胖',
      },
      {
        checked: false,
        name: '贫血',
      },
      {
        checked: false,
        name: '长期用药',
      },
      {
        checked: false,
        name: '内分泌疾病史',
      },
      {
        checked: false,
        name: '既往妊娠分娩有异常',
      },
      {
        checked: false,
        name: '妊娠早期出血',
      },
      {
        checked: false,
        name: '糖尿病',
      },
      {
        checked: false,
        name: '妊娠期3个月内病毒感染',
      },
      {
        checked: false,
        name: '妊娠期感染',
      },
      {
        checked: false,
        name: '妊高期',
      },
      {
        checked: false,
        name: '放射线腹部照射',
      },
    ],
    productionFactorsList: [
      {
        checked: false,
        name: '前置胎盘',
      },
      {
        checked: false,
        name: '胎盘早剥',
      },
      {
        checked: false,
        name: '羊水异常',
      },
      {
        checked: false,
        name: '脐带异常',
      },
      {
        checked: false,
        name: '产程长',
      },
      {
        checked: false,
        name: '剖腹产',
      },
      {
        checked: false,
        name: '产钳助产',
      },
      {
        checked: false,
        name: '臀位分娩',
      },
      {
        checked: false,
        name: '产钳分娩',
      },
    ],
    postnatalFactorsList: [
      {
        name: '早产',
      },
      {
        checked: false,
        name: '过期产',
      },
      {
        checked: false,
        name: '低出生体重儿',
      },
      {
        checked: false,
        name: '双胎',
      },
      {
        checked: false,
        name: '多胎',
      },
      {
        checked: false,
        name: '酸中毒',
      },
      {
        checked: false,
        name: '巨大儿',
      },
      {
        checked: false,
        name: '呼吸暂停',
      },
      {
        checked: false,
        name: '青紫发作',
      },
      {
        checked: false,
        name: '颅内出血',
      },
      {
        checked: false,
        name: '重症贫血',
      },
      {
        checked: false,
        name: '颅内感染',
      },
      {
        checked: false,
        name: '低血糖',
      },
      {
        checked: false,
        name: '抽搐',
      },
      {
        checked: false,
        name: '畸形',
      },
      {
        checked: false,
        name: '产伤',
      },
      {
        checked: false,
        name: '窒息缺氧',
      },
      {
        checked: false,
        name: '缺氧缺血性脑病',
      },
    ],
    patientInfo: '',
    options: {},
    files: [],
    videos: [],
    bottomHeight: app.kBottomSafeHeight + 43,
    stepHeight: app.kBottomSafeHeight + 266,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const patientInfo = parse(options.patient)
    console.log(app.kBottomSafeHeight, this.data.bottomHeight)
    this.setData({
      patientInfo: patientInfo,
    })
    this.getPatientDetial(patientInfo.id)
  },
  getPatientDetial: async function (id) {
    const res = await request.get(api.patientDetail + '/' + id)
    if (res.httpStatus === 200) {
      const productionFactorsList = this.data.productionFactorsList
      const prenatalFactorsList = this.data.prenatalFactorsList
      const postnatalFactorsList = this.data.postnatalFactorsList
      if (res.data.postnatalFactors) {
        const postnatalFactors = res.data.postnatalFactors.split(';')
        for (const i in postnatalFactors) {
          for (const j in postnatalFactorsList) {
            if (postnatalFactors[i] === postnatalFactorsList[j].name) {
              postnatalFactorsList[j].checked = true
            }
          }
        }
      }
      if (res.data.prenatalFactors) {
        const prenatalFactors = res.data.prenatalFactors.split(';')
        for (const i in prenatalFactors) {
          for (const j in prenatalFactorsList) {
            if (prenatalFactors[i] === prenatalFactorsList[j].name) {
              prenatalFactorsList[j].checked = true
            }
          }
        }
      }
      if (res.data.productionFactors) {
        const productionFactors = res.data.productionFactors.split(';')
        for (const i in productionFactors) {
          for (const j in productionFactorsList) {
            if (productionFactors[i] === productionFactorsList[j].name) {
              productionFactorsList[j].checked = true
            }
          }
        }
      }
      this.setData({
        postnatalFactorsList: postnatalFactorsList,
        prenatalFactorsList: prenatalFactorsList,
        productionFactorsList: productionFactorsList,
      })
    }
  },
  prenatalFactors(val) {
    const index = val.currentTarget.dataset.index
    this.data.prenatalFactorsList[index].checked =
      !this.data.prenatalFactorsList[index].checked
    this.setData({
      prenatalFactorsList: this.data.prenatalFactorsList,
    })
  },
  productionFactors(val) {
    const index = val.currentTarget.dataset.index
    this.data.productionFactorsList[index].checked =
      !this.data.productionFactorsList[index].checked
    this.setData({
      productionFactorsList: this.data.productionFactorsList,
    })
  },
  postnatalFactors(val) {
    const index = val.currentTarget.dataset.index
    this.data.postnatalFactorsList[index].checked =
      !this.data.postnatalFactorsList[index].checked
    this.setData({
      postnatalFactorsList: this.data.postnatalFactorsList,
    })
  },
  begin: async function () {
    const prenatalFactorsList = this.data.prenatalFactorsList
    const productionFactorsList = this.data.productionFactorsList
    const postnatalFactorsList = this.data.postnatalFactorsList
    let productionFactors = '', // 产中
      prenatalFactors = '', // 产前
      postnatalFactors = '' // 产后
    for (let i = 0; i < prenatalFactorsList.length; i += 1) {
      if (prenatalFactorsList[i].checked) {
        prenatalFactors = prenatalFactors + prenatalFactorsList[i].name + ';'
      }
    }
    for (let i = 0; i < productionFactorsList.length; i += 1) {
      if (productionFactorsList[i].checked) {
        productionFactors =
          productionFactors + productionFactorsList[i].name + ';'
      }
    }
    for (let i = 0; i < postnatalFactorsList.length; i += 1) {
      if (postnatalFactorsList[i].checked) {
        postnatalFactors = postnatalFactors + postnatalFactorsList[i].name + ';'
      }
    }
    const res = await request.post(api.highRisk, {
      id: this.data.patientInfo.id,
      productionFactors: productionFactors,
      prenatalFactors: prenatalFactors,
      postnatalFactors: postnatalFactors,
    })
    this.chooseVideo()
  },
  // 选择视频
  chooseVideo: function (e) {
    const that = this
    wx.chooseVideo({
      sourceType: ['album'], // album 从相册选视频，camera 使用相机拍摄
      // maxDuration: 60, // 拍摄视频最长拍摄时间，单位秒。最长支持60秒
      camera: 'back', //默认拉起的是前置或者后置摄像头，默认back
      compressed: true, //是否压缩所选择的视频文件
      success: function (res) {
        const file = {
          url: res.tempFilePath,
          coverUrl: res.thumbTempFilePath,
        }
        const typeItem = res.tempFilePath.split('.')
        if (typeItem[1] !== 'mp4') {
          wx.showModal({
            content: '请上传MP4格式的视频',
            cancelText: '取消', //默认是“取消”
            confirmText: '确认', //默认是“确定”
            success: function (res) {
              if (res.cancel) {
                //点击取消,默认隐藏弹框
              } else {
                //点击确定
              }
            },
            fail: function (res) {}, //接口调用失败的回调函数
            complete: function (res) {}, //接口调用结束的回调函数（调用成功、失败都会执行）
          })
          return
        }
        let duration = res.duration //选定视频的时间长度
        duration = parseInt(duration / 60) + ':' + parseInt(duration % 60)
        const tiem = duration.split(':')
        if (Number(tiem[0]) > 10 || Number(tiem[0]) < 2) {
          wx.showModal({
            content:
              '您选择的视频时长不符合要求，请选择时长大于2分钟小于10分钟的视频',
            cancelText: '取消', //默认是“取消”
            confirmText: '确认', //默认是“确定”
            success: function (res) {
              if (res.cancel) {
                //点击取消,默认隐藏弹框
              } else {
                //点击确定
              }
            },
            fail: function (res) {}, //接口调用失败的回调函数
            complete: function (res) {}, //接口调用结束的回调函数（调用成功、失败都会执行）
          })
          return
        }
        let newDuration

        if (Number(tiem[0]) > 0) {
          newDuration = tiem[0] + '分' + tiem[1] + '秒'
        } else {
          newDuration = tiem[1] + '秒'
        }
        const data = {
          file: file,
          duration: newDuration,
          id: that.data.patientInfo.id,
        }
        wx.navigateTo({
          url: `/pages/selfAssessment/uploadVideo/index?data=${stringify(
            data
          )}`,
        })
      },
      fail: function () {
        // fail
      },
      complete: function () {
        // complete
      },
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {},
})
